<?php

namespace App\Models\V2;

use DB;


class AudioModel
{

    private $condition;

    public function __construct()
    {
        $today           = date_time();
        $this->condition = [
            ['isShow', '=', 1],
            ['DeleteMark', '=', 1],
            ['PublishDate', '<=', $today]
        ];
    }

    /*
     * 获取所有的音频
     * */
    public function get_audio_all()
    {
        /*每日一言*/
        $audioList = DB::table('audioday')
            ->select('AudioID', 'AudioTitle', 'FilePath1', 'ReleaseDate', 'PublishDate', "view_times")
            ->where($this->condition)
            ->orderBy('PublishDate', 'desc')->get();
        foreach ($audioList as $v) {
            if (strtotime($v->PublishDate) > 1000)
                $v->ReleaseDate = $v->PublishDate;
        }
        return $audioList;

    }


    /**
     * 获取音频列表
     * @param $perNumber integer 每页显示的数量
     * */
    public function get_audio_list($perNumber=20)
    {
        /*每日一言*/
        $audioList = DB::table('audioday')
            ->select('AudioID', 'AudioTitle', 'FilePath1', 'ReleaseDate', 'PublishDate', "view_times")
            ->where($this->condition)
            ->orderBy('PublishDate', 'desc')
            ->paginate($perNumber);
        foreach ($audioList as $v) {
            if (strtotime($v->PublishDate) > 1000)
                $v->ReleaseDate = $v->PublishDate;
        }
        return $audioList;
    }


    /**
     * 获取音频信息
     * */
    public function get_audio_info($audioId)
    {
        $audioInfo = DB::table('audioday')
            ->where($this->condition)
            ->where('AudioID', $audioId)
            ->first();
        return $audioInfo;
    }


    /**
     * 修改音频信息
     * @param integer audioId 音频ID
     * @param array audioInfo 音频信息
     *
     * */
    public function update_audio($audioId, $audioInfo)
    {
        $result = DB::table('audioday')->where('AudioID', $audioId)->update($audioInfo);
        return $result;
    }

    /**
     * 增加音频播播放次数
     * */
    public function add_audio_view_times($audioId)
    {
        return DB::table('audioday')->where('AudioID', $audioId)->increment('view_times', 1);
    }


    /**
     * 当日音频信息
     * */
    public function get_current_day_video()
    {
        $date = date('Y-m-d');
        return DB::table('audioday')
            ->where('PublishDate', $date)
            ->where('IsShow', 1)
            ->where('DeleteMark', 1)
            ->first();
    }
}